package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DailyRecAdapter;
import flc.ast.databinding.ActivityDailyRecBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class DailyRecActivity extends BaseAc<ActivityDailyRecBinding> {

    private DailyRecAdapter dailyRecAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_daily_rec;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivDailyRecBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvDailyRecList.setLayoutManager(new LinearLayoutManager(mContext));
        dailyRecAdapter = new DailyRecAdapter();
        mDataBinding.rvDailyRecList.setAdapter(dailyRecAdapter);
        dailyRecAdapter.setOnItemClickListener(this);

        mDataBinding.refreshRecommendItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshRecommendItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshRecommendItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshRecommendItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshRecommendItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        //日历
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        setData(calendar, Calendar.MONDAY, format, getString(R.string.week_text1));
        setData(calendar, Calendar.TUESDAY, format, getString(R.string.week_text2));
        setData(calendar, Calendar.WEDNESDAY, format, getString(R.string.week_text3));
        setData(calendar, Calendar.THURSDAY, format, getString(R.string.week_text4));
        setData(calendar, Calendar.FRIDAY, format, getString(R.string.week_text5));
        setData(calendar, Calendar.SATURDAY, format, getString(R.string.week_text6));
        setData(calendar, Calendar.SUNDAY, format, getString(R.string.week_text7));
    }

    //设置日期并判断是否为今天
    private void setData(Calendar calendar, int mode, SimpleDateFormat format, String week) {
        calendar.set(Calendar.DAY_OF_WEEK, mode);
        if (TimeUtils.isToday(calendar.getTime())) {
            mDataBinding.tvMonth.setText("/" + (calendar.get(Calendar.MONTH) + 1));
            mDataBinding.tvDate.setText(calendar.get(Calendar.DAY_OF_MONTH) + "");
            mDataBinding.tvWeek.setText(week);
            getData();
        }
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.DAILY_REC_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (page == 1) {
                        dailyRecAdapter.setList(result);
                    } else {
                        dailyRecAdapter.addData(result);
                    }
                }
            }
        });
    }

}